package com.zhongkun.datahome.web.rest;

import com.zhongkun.datahome.common.annotation.Log;
import com.zhongkun.datahome.common.core.controller.BaseController;
import com.zhongkun.datahome.common.core.domain.AjaxResult;
import com.zhongkun.datahome.common.core.domain.R;
import com.zhongkun.datahome.common.core.page.TableDataInfo;
import com.zhongkun.datahome.common.enums.BusinessType;
import com.zhongkun.datahome.framework.manager.AsyncManager;
import com.zhongkun.datahome.service.domain.HouseMaterial;
import com.zhongkun.datahome.service.manager.ServiceAsyncFactory;
import com.zhongkun.datahome.service.req.cmd.material.HouseMaterialCmd;
import com.zhongkun.datahome.service.req.qry.house.CustomerHouseQry;
import com.zhongkun.datahome.service.req.qry.material.HouseMaterialQry;
import com.zhongkun.datahome.service.res.material.HouseMaterialRoomVo;
import com.zhongkun.datahome.service.res.material.HouseMaterialSkuVo;
import com.zhongkun.datahome.service.res.room.MaterialRoomVo;
import com.zhongkun.datahome.service.service.IHouseMaterialService;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 房产材料清单Controller
 * 
 * @author ruoyi
 * @date 2025-04-14
 */
@RestController
@RequestMapping("/core/material")
public class HouseMaterialController extends BaseController {
    @Resource
    private IHouseMaterialService houseMaterialService;

    @ApiOperation("查询空间列表")
    @PreAuthorize("@ss.hasPermi('core:material:room')")
    @GetMapping("/room")
    public R<List<String>> listRoom(Long houseId) {
        return R.ok(houseMaterialService.listRoom(houseId));
    }

    @ApiOperation("查询房产材料清单列表")
    @PreAuthorize("@ss.hasPermi('core:material:list')")
    @GetMapping("/list")
    public R<List<HouseMaterialRoomVo>> listBy(HouseMaterialQry qry) {
        return R.ok(houseMaterialService.listBy(qry));
    }


    @ApiOperation("批量保存房产材料清单")
    @PreAuthorize("@ss.hasPermi('core:material:batchSave')")
    @Log(title = "房产材料清单", businessType = BusinessType.UPDATE)
    @PostMapping("/batchSave")
    public R<Void> batchSave(@RequestBody HouseMaterialCmd cmd) {
        houseMaterialService.batchSave(cmd);
        return R.ok();
    }

    @ApiOperation("导入客户房产和清单")
    @PreAuthorize("@ss.hasPermi('core:material:import')")
    @Log(title = "房产材料清单", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public R<Void> upload(MultipartFile file) {
        List<Long> createdHouseIds = houseMaterialService.upload(file);
        // 生成房产二维码
        createdHouseIds.forEach(houseId -> AsyncManager.me().execute(ServiceAsyncFactory.createCustomerMiniProgram(houseId)));
        return R.ok();
    }

    @ApiOperation("导出客户房产和清单")
    @PreAuthorize("@ss.hasPermi('core:material:export')")
    @Log(title = "房产材料清单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(HttpServletResponse response, CustomerHouseQry qry) {
        houseMaterialService.export(qry, response);
    }
}
